<?php
/**
 * User: Allen
 * Date: 15-12-03
 * 管理员控制器
 */

class Admin extends PC_Controller{
	
	protected $go_url = '/admin/admin/admin_list';	
	public function __construct(){
		parent::__construct();
		$this->load->model('admin_model');
		$this->load->model('role_model');
	}
	
	/**
	 * @brief 管理员信息页面
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */	
	public function admin_list() {
		$message = 1;
		if($this->uri->segment(4)) {
			$message = $this->uri->segment(4);
		}
		$this->data['message'] = $message;
		//判断权限
		$this->data['authority'] = $this->authority;
		$this->load->view('/admin/admin_list.php',$this->data);
	}
	
	/**
	 * @brief ajxa获取文章分类信息
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function ajax_admin_list() {
		$data = $_GET;
		$start = $data['iDisplayStart']; //显示的起始索引
		$length = $data['iDisplayLength'];//显示的行数
		$sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列 
		$sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
		$search = $data ['sSearch'];//全局搜索字段
		$where['concat(id) like'] = '%'.trim($search).'%';
		$this->data['count'] = $this->admin_model->getCount($where);
		$aaData = array();
		if($this->data['count']) {
			$order_by = $sort_th." ".$sort_type;
			$admin_list = $this->admin_model->getAdminList($where,$length,$start,$order_by);
			$authority = $this->authority;
			foreach($admin_list as $key => $value) {
				$admin_list[$key]['check'] = '<label><input name="c_id" type="checkbox" value="'.$value['id'].'" /></label>';
				$edit_url = '';
				if($authority['edit_status'] == 2) {
					$edit_url = '<a href="/admin/admin/edit_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">编辑</span></a>';
				}
				$del_url = '';
				if($authority['del_status'] == 2) {
					$del_url = '<a onclick="deleteOne(this)"  data_id = '.$value['id'].'><span class="btn btn-xs btn-pink">删除</span></a>';
				}
				$admin_list[$key]['operate'] = $edit_url." ".$del_url;
				if(empty($del_url) && empty($edit_url)) {$admin_list[$key]['operate'] = '无';};
			}
			$aaData = $admin_list;
		}
		$output['aaData'] = $aaData;
		$output['sEcho'] = $_GET['sEcho'];
		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		echo json_encode($output); //最后把数据以json格式返回
	}
	
	/**
	 * @brief 编辑页面
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$role_list = $this->role_model->getRoleAll();
		$this->data['role_list'] = $role_list;
		$type_info = $this->admin_model->checkAdmin($where);
		$this->data['data'] = $type_info;
		$this->load->view('/admin/admin_edit',$this->data);
	}
			
	/**
	 * @brief 保存编辑信息
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function edit() {
		$data = $_POST;
		$where['id'] = $data['id'];
		$edit_result = $this->admin_model->editAdmin($where,$data);
		if($edit_result) {
			$this->location_href($this->go_url."/2");	
		}else {
			$this->location_href($this->go_url."/3");	
		}
	}
	
	/**
	 * @brief 检查管理员姓名唯一性
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function check_sole() {
		$data = $_POST;
		if(isset($data['id'])) {
			$where['id !='] = $data['id'];
		}
		$where['username'] = $data['username'];
		$sole = $this->admin_model->checkAdmin($where);
		if($sole) {
			$message = false;
		}else {
			$message = true;
		}
		echo json_encode($message);
	}
	
	/**
	 * @brief 删除信息
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function delete() {
		$data = $_POST;
		$id = $_POST['id'];
		$where['id'] = $id;	
		$del_result = $this->admin_model->deleteAdmin($where);
		echo json_encode($del_result);
	}	
	
	/**
	 * @brief 删除多行信息
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function delete_all() {
		$data = $_POST;
		$id_list = array_filter(explode(',',$data['str']));
		//$id_list = array(3,4);
		$del_result = $this->admin_model->deleteAll($id_list);
		echo json_encode($del_result);
	}
	
	/**
	 * @brief 进入添加页面
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function add_page() {
		$where['status'] = 1;
		$role_list = $this->role_model->getRoleAll($where);
		$this->data['role_list'] = $role_list;
		$this->load->view('/admin/admin_add',$this->data);
	}
	
	/**
	 * @brief 保存添加信息
	 * @param Null
	 * @par 2015/12/03 Ver 1.00 Created by Allen
	 */
	public function add() {
		$data = $_POST;	
		$add_result = $this->admin_model->addAdmin($data);
		if($add_result) {
			$this->location_href($this->go_url."/4");
		}else {
			$this->location_href($this->go_url."/5");
		}
	}	
}
?>